package com.dd.cloud.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dd.cloud.common.vo.PageReq;
import com.dd.cloud.user.req.news.AppPromotionNewsCreateReq;
import com.dd.cloud.user.req.news.AppPromotionNewsPageReq;
import com.dd.cloud.user.req.news.AppPromotionNewsUpdateReq;
import com.dd.cloud.user.res.news.AppPromotionNewsDetailRes;
import com.dd.cloud.user.res.news.AppPromotionNewsPageRes;
import com.dd.cloud.user.res.news.NewsInfoPageRes;
import com.dd.cloud.user.service.news.IAppPromotionNewsService;
import com.dd.cloud.user.service.news.INewsInfoService;
import com.dd.cloud.user.service.news.INewsSeeService;
import com.dd.cloud.user.util.JiGuangPushUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * @author Lx
 * @version 1.0
 */
@Validated
@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private INewsInfoService newsInfoService;
    @Autowired
    private INewsSeeService newsSeeService;
    @Autowired
    private IAppPromotionNewsService appPromotionNewsService;

    /**
     * 查询消息列表 分页
     *
     * @param req
     * @param status 读取状态 1未读 2已读
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<NewsInfoPageRes>> queryNewsInfoPage(@Valid PageReq req, @RequestParam(required = false) Integer status) {
        IPage<NewsInfoPageRes> res = newsInfoService.queryNewsInfoPage(req, status);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询消息未读数量
     *
     * @return
     */
    @GetMapping("/unread/count")
    public ResponseEntity<Integer> queryNewsSeeUnreadCount() {
        Integer res = newsSeeService.queryNewsSeeUnreadCount();
        return ResponseEntity.ok(res);
    }

    /**
     * 查阅消息
     *
     * @param newId
     */
    @PostMapping("/see")
    public ResponseEntity<Void> seeNewInfo(@NotNull(message = "消息id不能为空") Integer newId) {
        newsSeeService.seeNewInfo(newId);
        return ResponseEntity.ok().build();
    }

    /**
     * 创建app消息推广记录
     *
     * @param req
     */
    @PostMapping("/app/promotion/create")
    public ResponseEntity<Void> createAppPromotionNews(@RequestBody AppPromotionNewsCreateReq req) {
        appPromotionNewsService.createAppPromotionNews(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改app消息推广记录
     *
     * @param req
     */
    @PostMapping("/app/promotion/update")
    public ResponseEntity<Void> updateAppPromotionNews(@RequestBody AppPromotionNewsUpdateReq req) {
        appPromotionNewsService.updateAppPromotionNews(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询app消息推广记录
     *
     * @param req
     * @return
     */
    @GetMapping("/app/promotion/page")
    public ResponseEntity<IPage<AppPromotionNewsPageRes>> queryAppPromotionNewsPage(@Valid AppPromotionNewsPageReq req) {
        IPage<AppPromotionNewsPageRes> res = appPromotionNewsService.queryAppPromotionNewsPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 修改app消息推广记录状态
     *
     * @param id     消息id
     * @param status 状态 4草稿 -1删除
     */
    @PostMapping("/app/promotion/update/status")
    public ResponseEntity<Void> updateAppPromotionNewsStatus(@NotNull(message = "消息id不能为空") Integer id, @NotNull(message = "消息状态不能为空") Integer status) {
        appPromotionNewsService.updateAppPromotionNewsStatus(id, status);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询app消息推广记录详情
     *
     * @param id 消息id
     * @return
     */
    @GetMapping("/app/promotion/detail")
    public ResponseEntity<AppPromotionNewsDetailRes> queryAppPromotionNewsDetail(@NotNull(message = "消息id不能为空") Integer id) {
        AppPromotionNewsDetailRes res = appPromotionNewsService.queryAppPromotionNewsDetail(id);
        return ResponseEntity.ok(res);
    }

    /**
     * 重新推送/立即推送消息
     *
     * @param id 消息id
     */
    @PostMapping("/app/promotion/re/send")
    public ResponseEntity<Void> reSendAppPromotionNews(@NotNull(message = "消息id不能为空") Integer id) {
        appPromotionNewsService.reSendAppPromotionNews(id, 1);
        return ResponseEntity.ok().build();
    }

    /**
     * 推送消息成功收到
     *
     * @param id 消息id
     */
    @PostMapping("/app/promotion/receive/num")
    public ResponseEntity<Void> appPromotionNewsReceiveNum(@NotNull(message = "消息id不能为空") Integer id,
                                                           @NotBlank(message = "接收人id不能为空") String cid) {
        appPromotionNewsService.appPromotionNewsReceiveNum(id, cid);
        return ResponseEntity.ok().build();
    }

    /**
     * 推送消息被用户点击
     *
     * @param id 消息id
     */
    @PostMapping("/app/promotion/click/num")
    public ResponseEntity<Void> appPromotionNewsClickNum(@NotNull(message = "消息id不能为空") Integer id,
                                                         @NotBlank(message = "接收人id不能为空") String cid) {
        appPromotionNewsService.appPromotionNewsClickNum(id, cid);
        return ResponseEntity.ok().build();
    }

    /**
     * 测试推送
     *
     */
    @PostMapping("/app/test")
    public ResponseEntity<Void> test(@NotBlank(message = "消息标题不能为空") String title,
                                                         @NotBlank(message = "内容不能为空") String body,
                                     String[] cids) {
        JiGuangPushUtil.jpushPush(title, body, Arrays.asList(cids));
        return ResponseEntity.ok().build();
    }
}
